clear;
load channel_task_1.mat

N=64;
M=16;

FFT_NUM = 1024;
fn = 20e6*linspace(0,1,FFT_NUM);

hk = zeros(1,64);
for k=0:63
    sum1 = 0;
    for l=0:7
        sum1 = sum1 + h(l+1)*exp(-1j*2*pi*l*k/N);
    end
    hk(k+1) = sum1;
end

mapping = @(input) (input * -2 + 1);
demapping = @(input) (real(input) < 0);

errors = 0;
total = 0;
for it=0:0
    messagesize = 64*160;
    b = randi([0, 1], 1, messagesize);

    xk = mapping(b);
    [xm,xm2] = modulation(xk, M, N);
    %ym = conv(xm, h);
    ym = filter(h, [1], xm);
    %ym = xm;
    
    interference = pbj(size(ym, 2), -10);
    ym = ym + interference;
    
    %add noise
    %ym = awgn(ym, 10);
    
    [yk,yk2] = demodulation(ym, M, N);

    %yk = yk .* (1./hk);
    yk = equalizer(yk, hk, N);
    
    bout = demapping(yk);
    
    errors = errors + sum(abs(bout - b));
    total = total + messagesize;
end

BER = errors/total

%psd(bp_noise,2048)
%ym=bp_noise;

Pxx = abs(fft(ym,FFT_NUM)).^2/length(ym)/20e6;
Hpsd = dspdata.psd(Pxx(1:length(Pxx)/2),'Fs',20e6);
%plot(Hpsd);
